package com.yufubao.strat.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        // 填充创建时间和更新时间
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
        // 自动填充创建、修改人员id
        this.strictInsertFill(metaObject, "createManagerId", Long.class, StpUtil.getLoginIdAsLong());
        this.strictInsertFill(metaObject, "updateManagerId", Long.class, StpUtil.getLoginIdAsLong());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 更新时只填充更新时间和更新者
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());

        // 自动填充修改人员id
        this.strictUpdateFill(metaObject, "updateManagerId", Long.class, StpUtil.getLoginIdAsLong());
    }
}
